package com.kindsoft.demo.ad;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Service;

import static org.springframework.ldap.query.LdapQueryBuilder.query;

/**
 * Created by zhoujm on 2020/2/20/020.
 */
@Service
public class PersonRepository {
    @Autowired
    private LdapTemplate ldapTemplate;

    public Person create(Person person){
        ldapTemplate.create(person);
        return person;
    }

    public Person findByPrincipalName(String userPrincipalName){
//        String filter = "(&(objectCategory=Person)(userPrincipalName="+userPrincipalName+"))";
        return ldapTemplate.findOne(query().base("CN=Users").where("userPrincipalName").is(userPrincipalName), Person.class);
    }

    public Person modifyPerson(Person person){
        ldapTemplate.update(person);
        return person;
    }

    public void deletePerson(Person person){
        ldapTemplate.delete(person);
    }
}
